<?php

namespace App\Http\Model\Admin;

use Illuminate\Database\Eloquent\Model;

class Tourist extends Model
{
    //  table name
    protected $table = 'tourists';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'city', 'source_id', 'name', 'phone', 'status', 'purpose', 'use', 'remark',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];

    public function touristInfo()
    {
        return $this->hasOne(TouristInfo::class, 'phone', 'phone');
    }


    public function touristAction()
    {
        return $this->hasMany(TouristAction::class, 'user_id', 'id');
    }

    /**
     * find one user by id
     *
     * @param  id
     * @return  model
     */
    public function findOneById($id)
    {
        $one = self::find($id);
        return $one;
    }

    /**
     * find one by name
     *
     * @param  name
     * @return  model
     */
    public function findOneByName($name = '')
    {
        $one = self::where('name', $name)->firstOrFail();
        return $one;
    }

    /**
     * find list by pageinate default 15
     *
     * @param  per_page
     * @return page info with  model list
     */
    public function findAllByPaginate($per_page = 15, $input = [])
    {
        $list = [];
        if (!$input) {
            $list = self::orderBy('updated_at','desc')->paginate($per_page);;
        } else {
            /*
         *  tx  搜索词
         *  ct  城市
         *  sr  来源
         *  st  状态
         *  start 开始时间
         *  end   结束时间
         * */
            $one = new self();
            $l = [];
            $in_arr = [];
            if (isset($input['tx']) && !is_null($input['tx'])) {
                $l = $one->where('id', 'like', '%' . $input['tx'] . '%')->orWhere('name', 'like', '%' . $input['tx'] . '%')->orWhere('phone', 'like', '%' . $input['tx'] . '%');
            }
            if (isset($input['la']) && !is_null($input['la'])) {
                $ins = $l ? $l : $one;
                $l = $ins->where('admin_name',$input['la']);
            }
            if (isset($input['ct']) && !is_null($input['ct'])) {
                $ins = $l ? $l : $one;
                $l = $ins->whereIn('city', explode(',', $input['ct']));
            }
            if (isset($input['sr']) && !is_null($input['sr'])) {
                $sr_arr = explode(',', $input['sr']);
                foreach ($sr_arr as $key => $item){
                    $sr_arr[$key] = intval($item);
                }
                $ins = $l ? $l : $one;
                $l = $ins->whereIn('source_id', $sr_arr);
            }
            if (isset($input['st']) && !is_null($input['st'])) {
                $st_arr = explode(',', $input['st']);
                foreach ($st_arr as $key => $item){
                    $st_arr[$key] = intval($item);
                }
                $ins = $l ? $l : $one;
                $l = $ins->whereIn('status', $st_arr);
            }
            if(isset($input['start']) && !is_null($input['start'])){
                $ins = $l ? $l : $one;
                $l = $ins->where('updated_at','>',$input['start']);
            }

            if(isset($input['end']) && !is_null($input['end'])){
                $ins = $l ? $l : $one;
                $l = $ins->where('updated_at','<',$input['end']);
            }

            if ($l) {
                $list = $l->orderBy('updated_at','desc')->paginate($per_page);
            } else {
                $list = self::orderBy('updated_at','desc')->paginate($per_page);
            }
        }
        return $list;
    }

    /**
     * update one by id
     *
     * @param  id
     * @param  $input object
     * @return  boolean
     */
    public function updateOneById($id, $input)
    {
        return self::where('id', $id)->update($input);
    }


}
